package com.bacon.gmall.payment.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.bacon.gmall.common.config.RabbitConst;
import com.bacon.gmall.common.utils.RabbitUtil;
import com.bacon.gmall.model.enums.*;
import com.bacon.gmall.model.order.OrderDetail;
import com.bacon.gmall.model.order.OrderInfo;
import com.bacon.gmall.model.payment.PaymentInfo;
import com.bacon.gmall.payment.mapper.OrderDetailMapper;
import com.bacon.gmall.payment.mapper.OrderInfoMapper;
import com.bacon.gmall.payment.mapper.PaymentMapper;
import com.bacon.gmall.payment.service.PaymentService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @author： bacon、
 * @version： 1.0
 * @create： 2021-01-11 23:11
 * @description:
 */
@Service
public class PaymentServiceImpl implements PaymentService {

    @Autowired
    private PaymentMapper paymentMapper;

    @Autowired
    private OrderInfoMapper orderInfoMapper;

    @Autowired
    private RabbitUtil rabbitUtil;

    @Autowired
    private OrderDetailMapper orderDetailMapper;

    @Override
    public PaymentInfo insert(Long orderId) {

        //1、查询支付表
        //2、没有 生成支付表
        //3、有   直接使用
        PaymentInfo paymentInfo = paymentMapper.selectOne(new QueryWrapper<PaymentInfo>().eq("order_id", orderId));
        if (null == paymentInfo) {
            OrderInfo orderInfo = orderInfoMapper.selectById(orderId);
            paymentInfo = new PaymentInfo();
            paymentInfo.setCreateTime(new Date());
            paymentInfo.setOrderId(orderId);
            paymentInfo.setPaymentStatus(PaymentStatus.UNPAID.name());
            paymentInfo.setPaymentType(PaymentType.ALIPAY.name());
            paymentInfo.setOutTradeNo(orderInfo.getOutTradeNo());
            paymentInfo.setSubject(orderInfo.getTradeBody());
            paymentInfo.setTotalAmount(orderInfo.getTotalAmount());
            paymentMapper.insert(paymentInfo);
        }
        return paymentInfo;
    }

    //支付表更新
    @Override
    @Transactional
    public void updatePaymentInfo(Map<String, String> paramsMap) {

        //根据out_trade_no 查询信息
        PaymentInfo paymentInfo = paymentMapper.selectOne(new QueryWrapper<PaymentInfo>().
                eq("out_trade_no", paramsMap.get("out_trade_no")));
        if (null != paymentInfo) {
            //添加所缺的值，并更新订单状态
            paymentInfo.setTradeNo(paramsMap.get("trade_no"));
            paymentInfo.setPaymentType(PaymentStatus.UNPAID.name());
            paymentInfo.setCallbackTime(new Date());
            paymentInfo.setCallbackContent(JSONObject.toJSONString(paramsMap));
            paymentMapper.updateById(paymentInfo);

            OrderInfo orderInfo = orderInfoMapper.selectOne(new QueryWrapper<OrderInfo>().
                    eq("id", paymentInfo.getOrderId()));
            if (null != orderInfo && OrderStatus.UNPAID.name().equals(orderInfo.getOrderStatus())) {
                orderInfo.setProcessStatus(ProcessStatus.PAID.name());
                orderInfo.setOrderStatus(OrderStatus.PAID.name());
                orderInfoMapper.updateById(orderInfo);

                //发送MQ消息，通知仓库
                rabbitUtil.sendMessage(
                        RabbitConst.EXCHANGE_DIRECT_WARE_STOCK,
                        RabbitConst.ROUTING_WARE_STOCK,
                        JSONObject.toJSONString(initWareOrder(orderInfo.getId()))
                );
            }
        }
    }

    public Map initWareOrder(Long orderId) {

        //准备数据，添加到仓库数据库中
        Map<String, Object> map = new HashMap<>();
        OrderInfo orderInfo = orderInfoMapper.selectById(orderId);
        List<OrderDetail> orderDetailList = orderDetailMapper.selectList(new QueryWrapper<OrderDetail>().
                eq("order_id", orderId));

        //仓库订单表
        map.put("order_id", orderId);
        map.put("consignee", orderInfo.getConsignee());
        map.put("consigneeTel", orderInfo.getConsigneeTel());
        map.put("deliveryAddress", orderInfo.getDeliveryAddress()); //订单地址
        map.put("orderComment", orderInfo.getOrderComment()); //订单备注
        map.put("paymentWay", orderInfo.getPaymentWay().equals(PaymentWay.OUTLINE.name()) ? "2" : "1");
        map.put("orderBody", orderInfo.getTradeBody());

        //仓库详情表
        ArrayList<Map> mapArrayList = new ArrayList<>();
        for (OrderDetail orderDetail : orderDetailList) {
            Map<String, Object> orderDetailMap = new HashMap<>();
            map.put("sku_id",orderDetail.getSkuId());
            map.put("sku_name",orderDetail.getSkuName());
            map.put("sku_num",orderDetail.getSkuNum());
            mapArrayList.add(orderDetailMap);
        }

        map.put("details",mapArrayList);
        return map;
    }
}
